package com.mindcraft.media.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mindcraft.media.model.Dto.UploadFileParamsDto;
import com.mindcraft.media.model.Dto.UploadFileResultDto;
import com.mindcraft.media.model.po.MediaFiles;
import com.mindcraft.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
@Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
@RestController
@RequestMapping("/media")
public class UploadArticleController {

    @Autowired
    private MediaFileService mediaFileService;


    @ApiOperation("上传文件接口")
    @PostMapping("/uploadFile")
    public UploadFileResultDto upload(@RequestParam("file") MultipartFile filedata,@RequestHeader("Authorization") String userInfo) throws IOException {

        //准备上传文件的信息
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //原始文件名称
        uploadFileParamsDto.setFilename(filedata.getOriginalFilename());


        //文件大小
        uploadFileParamsDto.setFileSize(filedata.getSize());
        //文件类型
        uploadFileParamsDto.setFileType("001001");
        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        filedata.transferTo(tempFile);

        //文件路径
        String localFilePath = tempFile.getAbsolutePath();

        //调用service上传图片
        UploadFileResultDto uploadFileResultDto = mediaFileService.uploadFile(uploadFileParamsDto, localFilePath,userInfo);

        return uploadFileResultDto;
    }


}
